package homework18;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import sun.plugin.dom.core.Document;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;

public class UMLUtil {
    public static Object getBean(){

        try {
            //创建DOM文档
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            Document document ;
            document = (Document) documentBuilder.parse(new File("src//structuralPattern.adapterPattern.Adapter//confing.xml"));

            //获取包含类名的文本节点
            NodeList nodeList = document.getElementsByTagName("className");
            Node classNode = nodeList.item(0).getFirstChild();
            String cName = classNode.getNodeValue();

            //通过类名生成实例对象并将其返回
            Class c = Class.forName(cName);
            Object o = c.newInstance();
            return o;
        }
        catch (Exception e ){
            e.printStackTrace();
            return null;
        }
    }
}
